#include "GameBoard.h"
#include <windows.h>   // WinApi header
#include <iostream>
#include <cstdlib>

const int SMALL = 12;   //The size of a Small Board
const int MEDIUM = 17;  //The size of a Medium Board
const int LARGE = 22;   //The size of a Large Board

using std::cout;
using std::cin;
using std::endl;


int main()
{
		HANDLE mhConsole = GetStdHandle(STD_OUTPUT_HANDLE);
		bool stopplaying = false;
        int menu_selection = 0;
		int choice = 0;//To hold the user's choice of board size.
        bool done;      //To break from the menu loop
		char ch[2];
		char playAgain;
		int gameType = 0;
		char color_selection_char='z';
        //****************************************************************
        // Display the menu. Redisplay if menu_selection is invalid      *
        //****************************************************************

		cout << "What game type would you like to play?\n"
			<< "(1) User played\n"
			<< "(2) Computer Play-by-play\n"
			<< "(3) Statistical driver\n>>";
		cin >> gameType;
	do
	{
        do
        {
			system("cls");
			ch[0] = NULL;
			ch[1] = NULL;

					SetConsoleTextAttribute(mhConsole, 10);
                cout << "\n\tWelcome to our Flood-It solver! Please choose a board size:\n\n";
                     SetConsoleTextAttribute(mhConsole, 12);
				cout << "\t(1)\t Small Board  12x12\n";
                     SetConsoleTextAttribute(mhConsole, 2);
				cout << "\t(2)\t Medium Board 17x17\n";
                     SetConsoleTextAttribute(mhConsole, 14);
				cout << "\t(3)\t Large Board  22x22\n\n";
                     SetConsoleTextAttribute(mhConsole, 7);
				cout << ">>";

				cin>>ch[0];
				if(ch[1] == NULL)
				{
					menu_selection = atoi(ch);
				}
				done = false;

         //First we will convert the users menu_selection into the proper size of board. 
        switch(menu_selection)
			{
			case 1:
				choice = SMALL; //If they chose 1, it will become 12 for a 12x12 board.
				done = true;
				break;
			case 2:
				choice = MEDIUM; //If they chose 2, it will become 17 for a 17x17 board.
				done = true;
				break;
			case 3:
				choice = LARGE; //If they chose 3, it will become 22 for a 22x22 board.
				done = true;
				break;

			default:  
				system("cls");
				cout<< "You did not enter 1, 2, or 3.... Please try again  \n\n";
			}

        }while(done == false);
		done = false;	
		
		if(gameType == 3)
		{
			int ii = 0;
			cout << "\n\nHow many times would you like to iterate through the game?";
			cin >> ii;
			while(ii > 1)
			{
				GameBoard Board(choice, gameType);        //Create the board by passing in the size
				Board.GameDriver();
				--ii;
			}
		}
							//sets the 
			GameBoard Board(choice, gameType);        //Create the board by passing in the size
			Board.GameDriver();
			
		
		
		while(done == false)
		{
			cout << endl << endl << "Would you like to play again? Please enter either 'y' or 'n'\n>>";
			cin.sync();
			cin.get(playAgain);

			if(tolower(playAgain) == 'n')
			{
				stopplaying = true;
				done = true;
			}
			else if(tolower(playAgain) == 'y')
			{
				done = true;
			}
			else
				cout << "\n\nPlease enter either 'y' or 'n'";
		}

		}while(stopplaying == false);
		
        return 0;
}
